﻿Imports Llama
Imports System.Windows.Forms
Imports System.Threading

Public Class PIDContents
    ''' <summary>
    '''  DwgName()是所有需要生成目录的图纸名的ArrayList，PlantGroupName为目录图纸所在的Unit的Name,TempFileName为所需要使用的PID图框文件名包含后缀名，ContentDwgName为目录图纸的Drawing Name，ContentDwgNumber为目录图纸的Drawing Number。
    ''' </summary>
    Public Sub CreateDwgContent(ByRef DwgName As DataTable, PlantGroupName As String, TempFileName As String, ContentDwgName As String, ContentDwgNumber As String)

        Dim Datasource As New LMADataSource
        Dim DwgSPID(DwgName.Rows.Count - 1) As String
        Dim DwgCount As Integer
        Dim objdrawing1 As LMDrawing
        Dim objdrawings1 As New LMDrawings
        Dim strPlantGroupName As String
        Dim strTempFileName As String
        Dim strDrawingNumber As String
        Dim strDrawingName As String
        Dim objPIDAutoAPP As InteropPidAuto.Application
        Dim objPIDDrawing As InteropPidAuto.Drawing
        Dim objPlacement As New Plaice.Placement
        Dim strDrawingPropSymFile As String
        strDrawingPropSymFile = "\Design\Annotation\Labels\DrawingProperty.sym"
        Dim strFullDwgPropSymFile As String
        Dim strFrameSymfile As String
        strFrameSymfile = "\Design\Annotation\Labels\Frame.sym"
        Dim strTitleBlockFile As String
        strTitleBlockFile = "\Design\Title Block Label-A1-SNEC.sym"
        Dim strFullFrameSymFile As String
        Dim strSourceSymFile As String
        Dim objFrameSymbol As LMSymbol
        Dim objTitleBlockSymbol As LMSymbol
        Dim objdrawings2 As New LMDrawings
        Dim objdrawing2 As LMDrawing
        Dim objSymbol As LMSymbol
        Dim objSymbols As New LMSymbols
        Dim objItem As LMAItem
        Dim DelSuccess As Boolean
        Dim objFilter As New LMAFilter
        Dim objCriterion As New LMACriterion
        Dim objOptionSettings As New LMOptionSettings
        Dim objOptionSetting As LMOptionSetting
        Dim XFrame As Double = 0.1016                     '-----------框框的XY坐标
        Dim YFrame As Double = 0.520685298770347
        Dim XTitleLabel As Double = 0.83099889362474322    '-----------Title block label 的XY坐标
        Dim YTitleLabel As Double = 0.00999710806737042
        Dim objNoteSeqNo As LMSymbol
        Dim objNoteDwgName As LMSymbol
        Dim objNoteDwgNumber As LMSymbol
        Dim objNoteDwgTitle As LMSymbol
        Dim XNote As Double = 0.106184623573032     '-----------第一个ItemNote的坐标
        Dim YNote As Double = 0.509175218646018
        Dim XNoteMove As Double = 0.0127              '------ItemNote的X轴的迁移一格的量
        Dim YNoteMove As Double = 0.007605299    '------ItemNote的Y轴的迁移一格的量
        Dim i As Integer         '-----用于循环每张图纸
        Dim j As Integer         '-----用于循环每张图纸目录
        Dim K As Integer = 150  '-----一张目录中放置多少张图 
        Dim L As Integer = 0      '------用于判断输入的Unit是否存在
        Dim objDwgNameNote As LMItemNote
        Dim objDwgNumberNote As LMItemNote
        Dim objDwgTitleNote As LMItemNote
        'Dim objDrawingGetProp As LMDrawing
        Dim CheckFileExist As Boolean
        Dim objUnit As LMPlantGroup
        Dim objUnits As LMPlantGroups


        '----------------清理临时文件夹-----------------------------------

        On Error Resume Next
        Dim strInTmp$
        strInTmp = Environ("TEMP") & "\*.tmp"
        Kill(strInTmp)

        '----------------判断Symbol是否存在，Unit名是否正确----------------------------

        strPlantGroupName = PlantGroupName
        objCriterion.SourceAttributeName = "Name"
        objCriterion.ValueAttribute = "PID Template Path"
        objCriterion.Operator = "="
        objFilter.ItemType = "OptionSetting"
        objFilter.Criteria.Add(objCriterion)
        objOptionSettings.Collect(Datasource, Filter:=objFilter)
        objOptionSetting = objOptionSettings.Nth(1)
        strTempFileName = objOptionSetting.Attributes("Value").Value + "\" + TempFileName

        CheckFileExist = System.IO.File.Exists(strTempFileName)
        If CheckFileExist = False Then
            MsgBox("输入的Template文件不存在~~~")
            GoTo EndThisSub
        End If

        objCriterion.SourceAttributeName = "PlantGroupType"
        objCriterion.ValueAttribute = 65     '--
        objCriterion.Operator = "="
        objFilter.ItemType = "PlantGroup"
        objFilter.Criteria.Add(objCriterion)
        objUnits = New LMPlantGroups
        objUnits.Collect(Datasource, Filter:=objFilter)
        For Each objUnit In objUnits
            If objUnit.Name = strPlantGroupName Then
                L = 1
            End If
        Next
        If L = 0 Then
            MsgBox("所填Unit Name不存在~~~")
            GoTo EndThisSub
        End If

        '--------------------------将Symbol文件移动到指定位置-------------------------------       
        objCriterion.SourceAttributeName = "Name"
        objCriterion.ValueAttribute = "Catalog Explorer root path"
        objCriterion.Operator = "="
        objFilter.ItemType = "OptionSetting"
        objFilter.Criteria.Add(objCriterion)
        objOptionSettings.Collect(Datasource, Filter:=objFilter)
        objOptionSetting = objOptionSettings.Nth(1)
        strFullDwgPropSymFile = objOptionSetting.Attributes("Value").Value + "\" + strDrawingPropSymFile
        strFullFrameSymFile = objOptionSetting.Attributes("Value").Value + "\" + strFrameSymfile
        CheckFileExist = System.IO.File.Exists(strFullDwgPropSymFile)
        strSourceSymFile = System.Environment.CurrentDirectory + "\" + "DrawingProperty.sym"
        If CheckFileExist = False Then
            strSourceSymFile = System.Environment.CurrentDirectory + "\" + "DrawingProperty.sym"
            System.IO.File.Copy(strSourceSymFile, strFullDwgPropSymFile)
        End If
        CheckFileExist = System.IO.File.Exists(strFullFrameSymFile)
        If CheckFileExist = False Then
            strSourceSymFile = System.Environment.CurrentDirectory + "\" + "Frame.sym"
            System.IO.File.Copy(strSourceSymFile, strFullFrameSymFile)
        End If

        '---------------------------------ProgressBar-------------------------------------------

        Dim mythread As Thread
        mythread = New Thread(AddressOf ShowProgressBar)
        mythread.Start()


        '---------------根据DrawingName排序后，创建对应的SPID的数组----------------
        'Array.Sort(DwgName)
        'DwgName.Sort()

        For DwgCount = 0 To DwgName.Rows.Count - 1
            'objCriterion.SourceAttributeName = "Name"
            'objCriterion.ValueAttribute = DwgName(DwgCount)
            'objCriterion.Operator = "="
            'objFilter.ItemType = "Drawing"
            'objFilter.Criteria.Add(objCriterion)
            'objdrawings1.Collect(Datasource, Filter:=objFilter)
            'objdrawing1 = objdrawings1.Nth(1)

            'DwgSPID(DwgCount) = objdrawing1.Attributes("SP_ID").Value
            DwgSPID(DwgCount) = DwgName.Rows(DwgCount)("Name").ToString

        Next

        objPIDAutoAPP = New InteropPidAuto.Application
        objPIDAutoAPP.Visible = False
        objPIDAutoAPP.Activate()

        '--------------设置目录图纸的信息----------------

        For j = 0 To (DwgSPID.Length) \ K

            If DwgSPID.Length > K Then
                strDrawingName = ContentDwgName & (j + 1).ToString
                strDrawingNumber = ContentDwgNumber & (j + 1).ToString
            Else
                strDrawingName = ContentDwgName
                strDrawingNumber = ContentDwgNumber
            End If

            Datasource = New LMADataSource
            objCriterion.SourceAttributeName = "Name"
            objCriterion.ValueAttribute = strDrawingName
            objCriterion.Operator = "="
            objFilter.ItemType = "Drawing"
            objFilter.Criteria.Add(objCriterion)
            objdrawings2.Collect(Datasource, Filter:=objFilter)
            If objdrawings2.Count = 1 Then
                'objdrawing2 = objdrawings2.Nth(1)
                'objCriterion.SourceAttributeName = "SP_DrawingID"
                'objCriterion.ValueAttribute = objdrawing2.Attributes("SP_ID").Value
                'objCriterion.Operator = "="
                'objFilter.ItemType = "Symbol"
                'objFilter.Criteria.Add(objCriterion)
                'objSymbols.Collect(Datasource, Filter:=objFilter)
                'objPIDDrawing = objPIDAutoAPP.Drawings.OpenDrawing(strDrawingName)
                'For Each objSymbol In objSymbols
                '    objItem = Datasource.GetModelItem(objSymbol.ModelItemID).AsLMAItem()
                '    If objSymbol.FileName.ToString() = strDrawingPropSymFile Then
                '        DelSuccess = False
                '        DelSuccess = objPlacement.PIDDeleteItem(objItem)
                '    End If
                'Next
                'GoTo RePlaceItemNote

                objPIDAutoAPP.Quit()
                MsgBox("图纸已存在，请删除图纸再试~")
                GoTo EndThisSub
            End If

            objPIDDrawing = objPIDAutoAPP.Drawings.Add(strPlantGroupName, strTempFileName, strDrawingNumber, strDrawingName)

            '------放置框框Symbol----------------------
            objPlacement = New Plaice.Placement
            objFrameSymbol = objPlacement.PIDPlaceSymbol(strFrameSymfile, XFrame, YFrame)
            objTitleBlockSymbol = objPlacement.PIDPlaceSymbol(strTitleBlockFile, XTitleLabel, YTitleLabel)

            '---------放置Note---------------------------
            'RePlaceItemNote:
            If j = (DwgSPID.Length) \ K Then
                For i = 0 To (DwgSPID.Length) - (j * K) - 1
                    If i < 55 Then
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote, YNote - YNoteMove * i)
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove, YNote - YNoteMove * i)
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 4, YNote - YNoteMove * i)
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 9, YNote - YNoteMove * i)
                    ElseIf i < 110 Then
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 16.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 17.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 20.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 25.2, YNote - YNoteMove * (i - 55))
                    Else
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 16.2 * 2, YNote - YNoteMove * (i - 110))
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 33.4, YNote - YNoteMove * (i - 110))
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 36.4, YNote - YNoteMove * (i - 110))
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 41.4, YNote - YNoteMove * (i - 110))
                    End If
                    '------------------上面放置Note的坐标按照一格XNoteMove or YNoteMove来做偏移----------
                    'objDrawingGetProp = Datasource.GetDrawing(DwgSPID(i + j * K))

                    Datasource = New LMADataSource
                    Datasource.BeginTransaction()
                    objDwgNameNote = Datasource.GetItemNote(objNoteSeqNo.ModelItemID)
                    objDwgNameNote.Attributes("Note.body").Value = (i + 1).ToString

                    objDwgNameNote = Datasource.GetItemNote(objNoteDwgName.ModelItemID)
                    objDwgNameNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("Name").ToString '  objDrawingGetProp.Attributes("Name").Value

                    objDwgNumberNote = Datasource.GetItemNote(objNoteDwgNumber.ModelItemID)
                    objDwgNumberNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("DrawingNumber").ToString ' objDrawingGetProp.Attributes("DrawingNumber").Value

                    objDwgTitleNote = Datasource.GetItemNote(objNoteDwgTitle.ModelItemID)
                    objDwgTitleNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("Title").ToString ' objDrawingGetProp.Attributes("Title").Value
                    Datasource.CommitTransaction()

                Next

            Else
                For i = 0 To K - 1
                    If i < 55 Then
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote, YNote - YNoteMove * i)
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove, YNote - YNoteMove * i)
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 4, YNote - YNoteMove * i)
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 9, YNote - YNoteMove * i)
                    ElseIf i < 110 Then
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 16.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 17.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 20.2, YNote - YNoteMove * (i - 55))
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 25.2, YNote - YNoteMove * (i - 55))
                    Else
                        objNoteSeqNo = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 16.2 * 2, YNote - YNoteMove * (i - 110))
                        objNoteDwgName = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 33.4, YNote - YNoteMove * (i - 110))
                        objNoteDwgTitle = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 36.4, YNote - YNoteMove * (i - 110))
                        objNoteDwgNumber = objPlacement.PIDPlaceSymbol(strDrawingPropSymFile, XNote + XNoteMove * 41.4, YNote - YNoteMove * (i - 110))
                    End If
                    '------------------上面放置Note的坐标按照一格YNoteMove来做偏移----------
                    'objDrawingGetProp = Datasource.GetDrawing(DwgSPID(i + j * K))

                    Datasource = New LMADataSource
                    Datasource.BeginTransaction()
                    objDwgNameNote = Datasource.GetItemNote(objNoteSeqNo.ModelItemID)
                    objDwgNameNote.Attributes("Note.body").Value = (i + 1).ToString

                    objDwgNameNote = Datasource.GetItemNote(objNoteDwgName.ModelItemID)
                    objDwgNameNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("Name").ToString ' objDrawingGetProp.Attributes("Name").Value

                    objDwgNumberNote = Datasource.GetItemNote(objNoteDwgNumber.ModelItemID)
                    objDwgNumberNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("DrawingNumber").ToString ' objDrawingGetProp.Attributes("DrawingNumber").Value

                    objDwgTitleNote = Datasource.GetItemNote(objNoteDwgTitle.ModelItemID)
                    objDwgTitleNote.Attributes("Note.body").Value = DwgName.Rows(i + j * K)("Title").ToString ' objDrawingGetProp.Attributes("Title").Value
                    Datasource.CommitTransaction()

                Next
            End If
            objPIDDrawing.CloseDrawing(True)
        Next
        objPIDAutoAPP.Quit()
        mythread.Abort()
        MsgBox("图纸目录已生成")
EndThisSub:
        Datasource = Nothing
        objPIDAutoAPP = Nothing
        objPIDDrawing = Nothing
        objPlacement = Nothing
        objFrameSymbol = Nothing
        objNoteDwgName = Nothing
        objNoteDwgNumber = Nothing
        objNoteDwgTitle = Nothing

    End Sub

    Public Sub ShowProgressBar()
        Dim objForm As New FormWithBar
        objForm.StartPosition = FormStartPosition.CenterParent
        objForm.ShowDialog()
    End Sub

End Class
